<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Linux 命令- ln | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Linux 命令- ln</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ln 是 Linux 中又一个非常重要的命令，它的功能的功能是为某一个文件在另一个位置建立一个同步的连接。当需要在不同的目录，用到相同的文件时，就不需要在每个需要的目录下都放一个必须相同的文件，只要在某个固定的目录，放上该文件，然后在其他的目录下用 ln 命令连接（link）就可以，不必重复的占用磁盘空间。</p>
<h2 id="1-命令格式"><a href="#1-命令格式" class="headerlink" title="1.命令格式"></a>1.命令格式</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ln [参数] [源文件或目录] [目标文件或目录]</div></pre></td></tr></table></figure>
<h2 id="2-命令功能"><a href="#2-命令功能" class="headerlink" title="2.命令功能"></a>2.命令功能</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Linux 文件系统中，有所谓的连接（link），可以将其视为档案的别名。而连接有可分为两种：硬链接（hard link）与软连接（symbolic link），硬连接的意思是一个档案可以有多个名称，而软连接的方式则是产生一个特殊的档案，该档案的内容是指向另一个档案的位置。硬链接是存在同一个文件系统中，而软连接却可以跨越不同的文件系统。</p>
<h3 id="软连接"><a href="#软连接" class="headerlink" title="软连接"></a>软连接</h3><ol>
<li><p>软连接，一路径的形式存在。类似与 windows 系统中的快捷方式；</p>
</li>
<li><p>软连接可以跨文件系统，硬链接不可以；</p>
</li>
<li><p>软连接可以对一个不存在的文件名进行连接；</p>
</li>
</ol>
<p>4.软连接可以对目录进行连接</p>
<h3 id="硬链接"><a href="#硬链接" class="headerlink" title="硬链接"></a>硬链接</h3><ol>
<li><p>硬链接，以文件副本的形式存在。但不占用实际空间；</p>
</li>
<li><p>不允许给目录创建硬链接；</p>
</li>
<li><p>硬链接只有在同一个文件系统中才能创建。</p>
</li>
</ol>
<h3 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h3><ol>
<li><p>ln 命令会保持每一处连接文件的同步性，也就是说，不论改动了哪一处，其他的文件都会发生相同的变化；</p>
</li>
<li><p>ln 的连接又分软连接和硬链接两种，软连接就是 <code>ln -s [源文件] [目标文件]</code> ，它只会在选定的位置上生成一个文件的镜像，不回占用磁盘空间，硬链接 <code>ln [源文件] [目标文件]</code> ，没有参数 -s ，会在选定的文职上生成一个和源文件大小相同的文件，无论是软连接还是硬链接，文件都保持同步变化。</p>
</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ln 指令在连接文件或目录，如同时指定两个以上的文件或目录，且最后的目的地是一个存在的目录，则会把前面指定的所有文件或目录复制到该目录中。若同时指定多个文件或目录，且最后的目的地并非是有一个已存在的目录，则会出现错误信息。</p>
<h2 id="3-命令参数"><a href="#3-命令参数" class="headerlink" title="3.命令参数"></a>3.命令参数</h2><h3 id="必要参数"><a href="#必要参数" class="headerlink" title="必要参数"></a>必要参数</h3><ul>
<li>-b 删除，覆盖以前建立的链接</li>
<li>-d 允许超级用户制作目录的硬链接</li>
<li>-f 强制执行</li>
<li>-i 交互模式，文件存在则提示用户是否覆盖</li>
<li>-n 把符号链接视为一般目录</li>
<li>-s 软链接(符号链接)</li>
<li>-v 显示详细的处理过程</li>
</ul>
<h3 id="选择参数"><a href="#选择参数" class="headerlink" title="选择参数"></a>选择参数</h3><ul>
<li>-S “-S&lt;字尾备份字符串&gt; ”或 “–suffix=&lt;字尾备份字符串&gt;”</li>
<li>-V “-V&lt;备份方式&gt;”或“–version-control=&lt;备份方式&gt;”</li>
<li>–help 显示帮助信息</li>
<li>–version 显示版本信息</li>
</ul>
<h2 id="使用实例"><a href="#使用实例" class="headerlink" title="使用实例"></a>使用实例</h2><h3 id="实例1：给文件创建软连接"><a href="#实例1：给文件创建软连接" class="headerlink" title="实例1：给文件创建软连接"></a>实例1：给文件创建软连接</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ln -s log2013.log link2013</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root bin      61 11-13 06:03 log2013.log</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ln -s log2013.log link2013</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -&gt; log2013.log</div><div class="line">-rw-r--r-- 1 root bin      61 11-13 06:03 log2013.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为 log2013.log 文件创建软连接 link2013 ，如果 log2013.log 丢失，link2013 将失效。</p>
<h3 id="实例2：给文件创建硬连接"><a href="#实例2：给文件创建硬连接" class="headerlink" title="实例2：给文件创建硬连接"></a>实例2：给文件创建硬连接</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ln log2013.log ln2013</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -&gt; log2013.log</div><div class="line">-rw-r--r-- 1 root bin      61 11-13 06:03 log2013.log</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ln log2013.log ln2013</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -&gt; log2013.log</div><div class="line">-rw-r--r-- 2 root bin      61 11-13 06:03 ln2013</div><div class="line">-rw-r--r-- 2 root bin      61 11-13 06:03 log2013.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为 log2013.log 创建硬链接 ln2013 ，log2013.log 与ln2013 的各项属性相同。</p>
<h3 id="实例3：接上面两实例，连接完毕后，删除和重建连接源文件"><a href="#实例3：接上面两实例，连接完毕后，删除和重建连接源文件" class="headerlink" title="实例3：接上面两实例，连接完毕后，删除和重建连接源文件"></a>实例3：接上面两实例，连接完毕后，删除和重建连接源文件</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -&gt; log2013.log</div><div class="line">-rw-r--r-- 2 root bin      61 11-13 06:03 ln2013</div><div class="line">-rw-r--r-- 2 root bin      61 11-13 06:03 log2013.log</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># rm -rf log2013.log </span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -&gt; log2013.log</div><div class="line">-rw-r--r-- 1 root bin      61 11-13 06:03 ln2013</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># touch log2013.log</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -&gt; log2013.log</div><div class="line">-rw-r--r-- 1 root bin      61 11-13 06:03 ln2013</div><div class="line">---xrw-r-- 1 root bin  302108 11-13 06:03 log2012.log</div><div class="line">-rw-r--r-- 1 root root      0 12-07 16:19 log2013.log</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># vi log2013.log </span></div><div class="line">2013-01</div><div class="line">2013-02</div><div class="line">2013-03</div><div class="line">2013-04</div><div class="line">2013-05</div><div class="line">2013-06</div><div class="line">2013-07</div><div class="line">2013-08</div><div class="line">2013-09</div><div class="line">2013-10</div><div class="line">2013-11</div><div class="line">2013-12[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -&gt; log2013.log</div><div class="line">-rw-r--r-- 1 root bin      61 11-13 06:03 ln2013</div><div class="line">-rw-r--r-- 1 root root     96 12-07 16:21 log2013.log</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cat link2013 </span></div><div class="line">2013-01</div><div class="line">2013-02</div><div class="line">2013-03</div><div class="line">2013-04</div><div class="line">2013-05</div><div class="line">2013-06</div><div class="line">2013-07</div><div class="line">2013-08</div><div class="line">2013-09</div><div class="line">2013-10</div><div class="line">2013-11</div><div class="line">2013-12</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cat ln2013 </span></div><div class="line">hostnamebaidu=baidu.com</div><div class="line">hostnamesina=sina.com</div><div class="line">hostnames=<span class="literal">true</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.源文件被删除后，并没有影响硬链接文件；软连接文件在 conetos 系统下不断的闪烁，提示远未见已经不存在</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;2.重建源文件后，团连接不再闪烁提示，说明已经连接成功，找到了连接文件系统；重建后，硬链接文件并没有收到源文件影响，硬链接文件的内容还是保留了删除前源文件的内容，说明硬链接已经失效。</p>
<h3 id="实例4：将文件链接为另一个目录中的相同名字"><a href="#实例4：将文件链接为另一个目录中的相同名字" class="headerlink" title="实例4：将文件链接为另一个目录中的相同名字"></a>实例4：将文件链接为另一个目录中的相同名字</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ln log2013.log test3</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ln log2013.log test3</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -&gt; log2013.log</div><div class="line">-rw-r--r-- 1 root bin      61 11-13 06:03 ln2013</div><div class="line">-rw-r--r-- 2 root root     96 12-07 16:21 log2013.log</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test3</span></div><div class="line">[root@localhost test3]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 2 root root 96 12-07 16:21 log2013.log</div><div class="line">[root@localhost test3]<span class="comment"># vi log2013.log </span></div><div class="line">2013-01</div><div class="line">2013-02</div><div class="line">2013-03</div><div class="line">2013-04</div><div class="line">2013-05</div><div class="line">2013-06</div><div class="line">2013-07</div><div class="line">2013-08</div><div class="line">2013-09</div><div class="line">2013-10[root@localhost test3]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 2 root root 80 12-07 16:36 log2013.log</div><div class="line">[root@localhost test3]<span class="comment"># cd ..</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -&gt; log2013.log</div><div class="line">-rw-r--r-- 1 root bin      61 11-13 06:03 ln2013</div><div class="line">-rw-r--r-- 2 root root     80 12-07 16:36 log2013.log</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 test3 目录中创建了 log2013.log 的硬链接，修改 test3 目录中的 log2013.log 文件，同时也会同步到源文件</p>
<h3 id="实例5：给目录创建软连接"><a href="#实例5：给目录创建软连接" class="headerlink" title="实例5：给目录创建软连接"></a>实例5：给目录创建软连接</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ln -sv /opt/soft/<span class="built_in">test</span>/test3 /opt/soft/<span class="built_in">test</span>/test5</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">drwxr-xr-x 2 root root   4096 12-07 16:36 test3</div><div class="line">drwxr-xr-x 2 root root   4096 12-07 16:57 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test5</span></div><div class="line">[root@localhost test5]<span class="comment"># ll</span></div><div class="line">lrwxrwxrwx 1 root root 5 12-07 16:57 test3 -&gt; test3</div><div class="line">[root@localhost test5]<span class="comment"># cd test3</span></div><div class="line">-bash: <span class="built_in">cd</span>: test3: 符号连接的层数过多</div><div class="line">[root@localhost test5]<span class="comment"># </span></div><div class="line">[root@localhost test5]<span class="comment"># </span></div><div class="line">[root@localhost test5]<span class="comment"># ll</span></div><div class="line">lrwxrwxrwx 1 root root 5 12-07 16:57 test3 -&gt; test3</div><div class="line">[root@localhost test5]<span class="comment"># rm -rf test3</span></div><div class="line">[root@localhost test5]<span class="comment"># ll</span></div><div class="line">[root@localhost test5]<span class="comment"># ln -sv /opt/soft/test/test3 /opt/soft/test/test5</span></div><div class="line">创建指向“/opt/soft/<span class="built_in">test</span>/test3”的符号链接“/opt/soft/<span class="built_in">test</span>/test5/test3”</div><div class="line">[root@localhost test5]<span class="comment"># ll</span></div><div class="line">lrwxrwxrwx 1 root root 20 12-07 16:59 test3 -&gt; /opt/soft/<span class="built_in">test</span>/test3</div><div class="line">[root@localhost test5]<span class="comment"># </span></div><div class="line">[root@localhost test5]<span class="comment"># cd test3</span></div><div class="line">[root@localhost test3]<span class="comment"># ll</span></div><div class="line">总计 4</div><div class="line">-rw-r--r-- 2 root root 80 12-07 16:36 log2013.log</div><div class="line">[root@localhost test3]<span class="comment"># touch log2014.log</span></div><div class="line">[root@localhost test3]<span class="comment"># ll</span></div><div class="line">总计 4</div><div class="line">-rw-r--r-- 2 root root 80 12-07 16:36 log2013.log</div><div class="line">-rw-r--r-- 1 root root  0 12-07 17:05 log2014.log</div><div class="line">[root@localhost test3]<span class="comment"># cd ..</span></div><div class="line">[root@localhost test5]<span class="comment"># cd ..</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<ol>
<li><p>目录只能创建软连接      </p>
</li>
<li><p>目录创建链接必须用绝对路径，相对路径创建会不成功，会提示：符号连接的层数过多 ，这样的错误      </p>
</li>
<li><p>在连接目标目录中修改文件都会在源文件目录中同步变化</p>
</li>
</ol>

	

	
		<span class="different-posts"><a href="/2017/10/12/2. Linux 命令/37. Linux 命令- ln/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
